package com.example.zhu95.mytimber.fragments;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.adapters.LibraryViewPagerAdapter;

import static com.example.zhu95.mytimber.ConstantValue.GRID_VIEW;
import static com.example.zhu95.mytimber.ConstantValue.LINEAR_VIEW;
import static com.example.zhu95.mytimber.adapters.LibraryViewPagerAdapter.Section;

/**
 * @author zhu95
 */
public class LibraryFragment extends BaseFragment {

    Section[] sections;
    Menu menu;
    LibraryViewPagerAdapter pagerAdapter = null;
    TabLayout tabLayout;
    ViewPager viewPager;

    public LibraryViewPagerAdapter getPagerAdapter() {
        return pagerAdapter;
    }

    ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            updateOptionsMenu(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    public LibraryFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sections = new Section[]{
                new Section(getResources().getString(R.string.songs), new SongsFragment()),
                new Section(getResources().getString(R.string.album), new AlbumFragment()),
                new Section(getResources().getString(R.string.artist), new ArtistFragment())};
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             final Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_library, container, false);

        Toolbar toolbar = view.findViewById(R.id.toolbar);
        rootActivity.setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) rootActivity.findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                rootActivity, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        toolbar.setTitle(R.string.app_name);
        //加上这句话，menu才会显示出来
        setHasOptionsMenu(true);

        tabLayout = view.findViewById(R.id.tab_layout);
        viewPager = view.findViewById(R.id.view_pager);
        // MARK:2017/12/12 fragment中嵌套fragment时使用getChildFragmentManager方法
        //  if (pagerAdapter == null) {
            pagerAdapter = new LibraryViewPagerAdapter(getChildFragmentManager(), sections);
        // }

        //设置适配器
        viewPager.setAdapter(pagerAdapter);
        viewPager.addOnPageChangeListener(pageChangeListener);
        //TabLayout关联ViewPager
        tabLayout.setupWithViewPager(viewPager);
        //设置模式 固定显示
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        this.menu = menu;
        updateOptionsMenu(viewPager.getCurrentItem());
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        int currentItem = viewPager.getCurrentItem();
        Fragment fragment = pagerAdapter.getItem(currentItem);

        if (itemId == R.id.view_list) {
            if (currentItem == 1) {
                ((AlbumFragment) fragment).setRecyclerViewMode(LINEAR_VIEW);
            } else if (currentItem == 2) {
                ((ArtistFragment) fragment).setRecyclerViewMode(LINEAR_VIEW);
            }
        } else if (itemId == R.id.view_grid) {
            if (currentItem == 1) {
                ((AlbumFragment) fragment).setRecyclerViewMode(GRID_VIEW);
            } else if (currentItem == 2) {
                ((ArtistFragment) fragment).setRecyclerViewMode(GRID_VIEW);
            }
        }
        fragment.onOptionsItemSelected(item);
        return super.onOptionsItemSelected(item);
    }


    private void updateOptionsMenu(int position) {
        MenuItem[] menuItems = new MenuItem[]{
                menu.findItem(R.id.action_viewas),
                menu.findItem(R.id.sort_year),
                menu.findItem(R.id.sort_artist),
                menu.findItem(R.id.sort_album),
                menu.findItem(R.id.sort_duration),
                menu.findItem(R.id.sort_songnum),
                menu.findItem(R.id.sort_albumnum)
        };
        boolean[][] visibility = {
                new boolean[]{false, true, true, true, true, false, false},
                new boolean[]{true, true, true, false, false, true, false},
                new boolean[]{true, false, false, false, false, true, true}
        };

        for (int i = 0; i < menuItems.length; i++) {
            menuItems[i].setVisible(visibility[position][i]);
        }
    }


}
